package com.knife.hospital.service.user;
import com.knife.hospital.config.BaseException;
import com.knife.hospital.constant.ResponseCode;
import com.knife.hospital.entity.UserPO;
import com.knife.hospital.mapper.UserPOMapper;
import org.springframework.stereotype.Service;

import com.knife.hospital.model.user.UserLoginResponse;
import com.knife.hospital.model.user.UserLoginRequest;
import com.knife.hospital.util.user.UserLoginUtil;
import javax.annotation.Resource;

import java.util.List;
@Service
public class UserLoginServiceImpl implements UserLoginService{
    @Resource
    private UserPOMapper userPOMapper;
    @Resource
    private UserLoginUtil userLoginUtil;

    public UserPO selectByNameAndPassword(String userName, String password) {
        return null;
    }
    public UserLoginResponse userLoginFunction(UserLoginRequest userLoginRequest) {
        try {
            List<UserPO> userPOs = userPOMapper.selectByNickname(userLoginRequest.getNickname());
            if (userPOs.size()==0)
                {
                    throw new BaseException(ResponseCode.USER_MISS);
                }
            else
            {
                UserPO userPO=userPOs.get(0);
                if (userPO.getPassword().equals(userLoginRequest.getPassword())){
                    System.out.println(userPO.getNickname());
                    UserLoginResponse userLoginResponse = userLoginUtil.userPoToUserResponse(userPO);
                    return userLoginResponse;
                }
                else
                    throw new BaseException(ResponseCode.PASSWORD_ERROR);
            }
        } catch (Exception e) {
            throw e;
        }
    }
}
